package sample.plugin.editor.activation.setting;

import android.view.View;
import android.widget.Toast;

import com.rapid.api.component.page.setting.SettingsPage;
import com.rapid.api.component.widget.item.ISettingGroupView;
import com.rapid.api.component.widget.item.ITextArrowItemView;
import com.rapid.api.framework.common.setting.AsSubSettingsInfo;
import com.rapid.api.framework.common.setting.BaseSettingsBuilder;
import com.rapid.framework.data.SharedPreferences;

public class ActivateSettingsBuilder extends BaseSettingsBuilder {
    public final static String KEY_SHADE = "ACTIVATE_EDITOR_SHADE";
    public final static String KEY_FIRE = "ACTIVATE_EDITOR_FIRE";
    public final static String KEY_VOICE = "ACTIVATE_EDITOR_VOICE";
    public final static String KEY_VOICE_FILE = "ACTIVATE_EDITOR_VOICE_FILE";
    public final static String KEY_LINE_VOICE = "ACTIVATE_EDITOR_LINE_VOICE";
    public final static String KEY_LINE_VOICE_FILE = "ACTIVATE_EDITOR_LINE_VOICE_FILE";
    public final static String KEY_VIBRATE = "ACTIVATE_EDITOR_VIBRATE";

    private final static String[] AUDIO_SUFFIXES = {"wav", "ogg", "mp3"};

    private ITextArrowItemView voiceView;
    private ITextArrowItemView newLineVoiceView;

    @Override
    public String getParentId() {
        return ID_EDITOR;
    }

    @Override
    public String getId() {
        return "editorActivate";
    }

    @Override
    public AsSubSettingsInfo getAsSubSettingsInfo() {
        return null;
    }

    @Override
    public void build(SettingsPage settingsPage) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        ISettingGroupView titleView = settingsPage.getOrAddSettingGroup("效果设置");
        titleView.addSwitchItem("开启抖动", "开启编辑器抖动效果", KEY_SHADE,
                sharedPreferences.getBoolean(KEY_SHADE, true));
        titleView.addSwitchItem("开启烟花", "开启编辑器烟花效果", KEY_FIRE,
                sharedPreferences.getBoolean(KEY_FIRE, true));

        titleView.addSwitchItem("敲击声", "开启编辑器输入时键盘敲击声效果", KEY_VOICE,
                sharedPreferences.getBoolean(KEY_VOICE, false));
        String voicePath = sharedPreferences.getString(KEY_VOICE_FILE);
        this.voiceView = titleView.addArrowItem("选择敲击声音频",
                voicePath == null ? "默认" : voicePath,
                "",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openFileSelector(AUDIO_SUFFIXES, s -> handleVoice(s));
                    }
                });

        titleView.addSwitchItem("换行音效", "开启编辑器换行输入时播放音效", KEY_LINE_VOICE,
                sharedPreferences.getBoolean(KEY_LINE_VOICE, false));
        String lineVoicePath = sharedPreferences.getString(KEY_VOICE_FILE);
        this.newLineVoiceView = titleView.addArrowItem("选择换行音效音频",
                lineVoicePath == null ? "默认" : lineVoicePath,
                "",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openFileSelector(AUDIO_SUFFIXES, s -> handleNewLineVoice(s));
                    }
                });

        titleView.addSwitchItem("震动效果", "开启编辑器输入时震动效果", KEY_VIBRATE,
                sharedPreferences.getBoolean(KEY_VIBRATE, false));
    }

    @Override
    public void dispose() {
        voiceView = null;
        newLineVoiceView = null;
    }

    private void handleVoice(String path) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        sharedPreferences.putString(KEY_VOICE_FILE, path);
        sharedPreferences.commit();
        voiceView.setDescription(path);
    }

    private void handleNewLineVoice(String path) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        sharedPreferences.putString(KEY_LINE_VOICE_FILE, path);
        sharedPreferences.commit();
        newLineVoiceView.setDescription(path);
    }
}
